﻿using System;
using System.Collections.Generic;
using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Libs;
using Yoozoo.Managers;
using Yoozoo.UI.YGUI;

namespace Gameplay.PVE.Survivor
{
    public class HudTeam : IClass
    {
        private BattleHudObject hudObject;

        private GameObject root;
        private YImage progressBar;
        private YText progressText;
        private YText progressTitle;

        public void Initialize(TeamGroup team)
        {
            hudObject = BattlePoolManager.Instance.GetHud((int)EBattleResId.TeamHud);
            hudObject.SetPrefabReadyAction(o =>
            {
                o.transform.SetParent(team.Transform);
                o.transform.localPosition = new Vector3(0, 2.74f, 0);
                o.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
                root = o.transform.Find("bg").gameObject;
                root.SetActive(false);
                
                progressBar = o.transform.Find("bg/bgBar/progressBar").GetComponent<YImage>();
                progressText = o.transform.Find("bg/textProgress").GetComponent<YText>();
                progressTitle = o.transform.Find("bg/textTitle").GetComponent<YText>();
            });
        }

        public void Show(string title)
        {
            root.SetActive(true);
            progressTitle.text = title;
            hudObject.Forward = CameraManager.Instance.CameraForward;
        }

        public void Hide()
        {
            root.SetActive(false);
        }
        
        public void UpdateProgress(float restTime,float totalTime)
        {
            progressBar.fillAmount = restTime / totalTime;
            //把restTime变为00:00的格式
            progressText.text = BattleHelper.GetTimeString(restTime);
        }
        
        public void OnReset()
        {
           
        }

        public void Dispose()
        {
            if (hudObject != null)
            {
                BattlePoolManager.Instance.ReleaseHud(hudObject);
                hudObject = null;
            }
        }
    }
}
